$(document).ready(function(){
	var token=localStorage.getItem("token");
	var id = tl_config.his_url;
	id = id.substring(id.indexOf("id=")+3, id.length);
	id = id.substring(0, id.indexOf("&"));
	$("#menu").removeClass().addClass("l_query");
	$("#precinct").removeClass().addClass("l_charge");
	$(".z_gangwei").empty();
	$.ajax({
		url:server_url+"rest/menuAuthority.do?timeStamp="+new Date().getTime(),
		type:"get",
		data:{
			postId:id,
			token:token
		},
		success:function(data){
			if(data.retCode=='0000'){
				var settings={
					keyName:"menuId",
					parentKeyName:"parentId",
					childKeyName:"children"
				};
				//三级菜单id和对应的功能操作
				var opMenu=data.data.opMenu;

				var nodes=data.data.sysMenu;
				var t=$.parseJSON(transformTozTreeFormat(settings,nodes));
				var menuArr=new Array();
				//遍历树结构直至叶子节点，获取从一级菜单到叶子节点的菜单名，以逗号分隔的形式保存在数组中
				//若有三级菜单，则再保存三级菜单id
				//例["系统名称","一级菜单名","二级菜单名","三级菜单名","postId"]
				for(var i in t){
					var tmp1=t[i];
					if(tmp1.children!=''&&tmp1.children!=undefined){
						for(var j in tmp1.children){
							var tmp2=tmp1.children[j];
							if(tmp2.children!=''&&tmp2.children!=undefined){
								for(var k in tmp2.children){
									var tmp3=tmp2.children[k];
									menuArr.push(tmp1.sysName+","+tmp1.menuName+","+tmp2.menuName+","+tmp3.menuName+","+tmp3.menuId);
								}
							}else{
								menuArr.push(tmp1.sysName+","+tmp1.menuName+","+tmp2.menuName);
							}
						}
					}else{
						menuArr.push(tmp1.sysName+","+tmp1.menuName);
					}
				}
				var html_str='';
				html_str+='<table>';
				html_str+='<tr>';
				html_str+='<th width="180">系统名称</th>';
				html_str+='<th width="160">一级菜单</th>';
				html_str+='<th width="160">二级菜单</th>';
				html_str+='<th width="160">三级菜单</th>';
				html_str+='<th width="290">功能名称</th>';
				html_str+='</tr>';
				for(var i in menuArr){
					var tmp=menuArr[i].split(",");
					//对应的菜单名存在，则显示
					html_str+='<tr>';
					html_str+='<td>'+(tmp[0]!=null&&tmp[0]!=undefined?tmp[0]:"")+'</td>';
					html_str+='<td>'+(tmp[1]!=null&&tmp[1]!=undefined?tmp[1]:"")+'</td>';
					html_str+='<td>'+(tmp[2]!=null&&tmp[2]!=undefined?tmp[2]:"")+'</td>';
					html_str+='<td>'+(tmp[3]!=null&&tmp[3]!=undefined?tmp[3]:"")+'</td>';
					html_str+='<td>';
					//如果三级菜单存在，则显示已分配的功能操作
					if(tmp[3]!=null&&tmp[3]!=undefined){
						for(var i in opMenu){
							if(opMenu[i].menuId==tmp[4]&&opMenu[i].opId!=null){
								html_str+=opMenu[i].opName+" ";
							}
						}
					}
					html_str+='</td>';
					html_str+='</tr>';
				}
				html_str+='</table>';
				html_str+='<a href="javascript:void(0);" class="z_goback l_sure">返回</a>';
				$("div.z_gangwei").html(html_str);

				$(".l_sure").click(function(){
					var url='postList.do?ran='+Math.random();
					tl_config.loadToWorkArea(url);
				});
			}else {
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		},
	});
});